package com.demo.core.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.demo.base.cons.SessionKeyConstant;
import com.demo.base.model.UserInfo;
import com.demo.base.util.RequestContextHolderUtil;

public class SessionContext {

	public static Map<Integer, HttpSession> sessionMap;

	public static Map<Integer, String> sidMap;

	public static List<String> listSessionId = new ArrayList<String>();

	/**
	 * 新用户登录后 添加到session中
	 * @param userid
	 * @param sid
	 * @param session
	 */
	public static void addSessionUser(Integer userid, String sid, HttpSession session) {
		if (sessionMap == null) {
			sessionMap = new HashMap<Integer, HttpSession>();
		}
		if (sidMap == null) {
			sidMap = new HashMap<Integer, String>();
		}
		sessionMap.put(userid, session);
		sidMap.put(userid, sid);
	}

	/**
	 * 用户注销时 或者同一用户在其他地方登录 从session中移除
	 * @param userid
	 */
	@SuppressWarnings("unchecked")
	public static void removeSessionUser(Integer userid) {
		if (sessionMap == null) {
			sessionMap = new HashMap<Integer, HttpSession>();
		}
		if (sidMap == null) {
			sidMap = new HashMap<Integer, String>();
		}
		String sid = sidMap.get(userid);
		HttpSession session = sessionMap.get(userid);
		if (sid != null && session != null && listSessionId.contains(session.getId())) {
			Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>) session.getAttribute(SessionKeyConstant.SESSION_CONTEXT);
			if (map == null) {
				map = new HashMap<String, Map<String, Object>>();
			}
			Map<String, Object> m = map.get(SessionKeyConstant.USER_CONTEXT);
			m.remove(sid);
		}
		sessionMap.remove(userid);
		sidMap.remove(userid);
	}

	/**
	 * 获取保存在Session中的对象
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Object getSessionObj(String key) {
		Object obj = null;
		Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>) RequestContextHolderUtil.getSession().getAttribute(SessionKeyConstant.SESSION_CONTEXT);
		if (map == null) {
			map = new HashMap<String, Map<String, Object>>();
		}
		Map<String, Object> m = map.get(key);
		if (m == null) {
			m = new HashMap<String, Object>();
		}
		String sid = RequestContextHolderUtil.getRequest().getParameter("sid");
		if (map != null && sid != null) {
			obj = m.get(sid);
		}
		return obj;
	}

	/**
	 * 获取保存在Session中的登录人员信息
	 * @return
	 */
	public static UserInfo getSessionUser() {
		return (UserInfo) SessionContext.getSessionObj(SessionKeyConstant.USER_CONTEXT);
	}

	/*
	 * private static SessionContext instance;
	 * 
	 * private SessionContext(){}
	 * 
	 * private Map<String, Map<String, Object>> context;
	 * 
	 * public static SessionContext getInstance(){ if(instance==null){
	 * instance=new SessionContext(); } return instance; }
	 * 
	 * public void addSessionMap(String key,Map<String, Object> value){
	 * if(context==null){ context=new HashMap<String, Map<String,Object>>();
	 * }else{ context.put(key, value); } }
	 * 
	 * public void removeSessionMap(String key){ if(context!=null){
	 * context.remove(key); } }
	 * 
	 * public Map<String, Object> getSessionMap(String key){ if(context!=null){
	 * return context.get(key); }else{ return null; } }
	 */
}
